home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / tutor / pro12 / instr.bas < prev    next >
BASIC Source File  |  1990-10-15  |  2KB  |  54 lines

  1. 10 'INSTR.BAS - Demonstration program to demonstrate!the use of the INSTR
  2. 20 'function
  3. 30 '
  4. 40 'From the GW-BASIC Tutorial Series (GWBT05, 10/15/1990)
  5. 50 '
  6. 60 'The program will use the following variables:
  7. 70 '
  8. 80 'INPUTSTRING$        is the string the user provides
  9. 90 'SEARCHSTRING$       is the string we want to search for
  10. 100 'POSITION           is the current search position
  11. 110 '
  12. 120 'What we will do is start at POSITION = 1, and search through the INPUT-
  13. 130 'STRING$ for ALL the times that SEARCHSTRING$ can be found, and report
  14. 140 'the position each time.
  15. 150 '
  16. 160 'Start of main program:
  17. 170 KEY OFF
  18. 180 CLS
  19. 190 PRINT "INSTR.BAS - INSTR Demonstration Program"
  20. 200 PRINT
  21. 210 PRINT "First, I need the string you want to search inside of.  Enter a"
  22. 220 PRINT "sentence, group of words, etc.  Press ENTER alone to quit."
  23. 230 LINE INPUT "Your string =>";INPUTSTRING$
  24. 240 IF INPUTSTRING$="" THEN END
  25. 250 PRINT
  26. 260 PRINT "Next, I need the string you want to LOOK FOR inside the first"
  27. 270 PRINT "string.  This may be a letter, space, word, etc."
  28. 280 PRINT
  29. 290 LINE INPUT "Search string =>";SEARCHSTRING$
  30. 300 '
  31. 310 'We have both strings, now start searching...
  32. 320 '
  33. 330 POSITION = 1
  34. 340 LOCATION=INSTR(POSITION,INPUTSTRING$,SEARCHSTRING$)
  35. 350 '
  36. 360 'If it is found, LOCATION will be greater than zero.  If it cannot be found
  37. 370 'then LOCATION will be zero.
  38. 380 IF LOCATION > 0 THEN GOTO 430
  39. 390 PRINT "No further matches found.  Press any key to try new strings..."
  40. 400 WHILE INKEY$="":WEND
  41. 410 GOTO 180
  42. 420 '
  43. 430 'Match found, now tell where it was
  44. 440 '
  45. 450 PRINT "A match was found at position ";LOCATION
  46. 460 '
  47. 470 'Now add 1 to the current location, make this the start position, and look
  48. 480 'again for any more locations of the search string...
  49. 490 '
  50. 500 POSITION=LOCATION + 1
  51. 510 GOTO 340
  52. 520 END
  53. 530 'End of program - INSTR.BAS
  54.